﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Common.Utilities;
using LDTB.Common.Entities;

namespace LDTB.Common
{
    public class UIBase :PortalModuleBase
    {
        private static LDTBDataContext data = null;
        public static LDTBDataContext Data
        {
            get
            {
                if (Null.IsNull(data)) data = new LDTBDataContext(Config.GetConnectionString());
                return data;
            }
        }

    
        public string WriteUrl(string tabid, string detail, string newid, string title)
        {

            int _tabid;
            int.TryParse(tabid, out _tabid);
            string url = DotNetNuke.Common.Globals.NavigateURL(_tabid == 0 ? this.TabId : _tabid, "", detail, newid, "title=" + WriteString(title));
            return url;
        }

        public string WriteString(string str)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            str = System.Text.RegularExpressions.Regex.Replace(str, "Đ|đ|&#273;|&#272;", "d", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            str = str.Normalize(System.Text.NormalizationForm.FormKD);
            for (int i = 0; i < str.Length; i++)
            {
                if (char.IsWhiteSpace(str[i])) sb.Append('-');
                else if (char.GetUnicodeCategory(str[i]) != System.Globalization.UnicodeCategory.NonSpacingMark
                    && !char.IsPunctuation(str[i])
                    && !char.IsSymbol(str[i]))
                    sb.Append(str[i]);
            }
            return sb.ToString();

        }        //thiet lap nay dung cho khi su ly tu fil Handler.ashx

        private int PageSizeDefault
        {
            get
            {
                return 10;
            }
        }


        public int PageSize
        {
            get
            {
                int _size = PageSizeDefault;
                if (Settings["PageSize"] != null)
                {
                    if (!int.TryParse(Settings["PageSize"].ToString(), out _size) || _size < 1)
                    {
                        _size = PageSizeDefault;
                    }
                }
                return _size;
            }
        }

        public int PageCurr
        {
            get
            {
                int currentPage = Request["page"] == null ? 1 : int.Parse(Request["page"].ToString());
                return currentPage;
            }
        }

    }
}
